import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _isEmpty from "lodash/isEmpty";
import _isArray from "lodash/isArray";
import { getResponse } from 'utils/utils';
import { queryIdpValue } from 'cpaas-front/lib/services/api';
import { fetchReceiveConfig as _fetchReceiveConfig, saveConfig as _saveConfig, editConfig as _editConfig, deleteConfig as _deleteConfig, renderTreeData } from "../services/receiveConfigService";
export default {
  namespace: 'receiveConfig',
  state: {
    messageTypeList: [],
    configList: [],
    // 消息配置列表
    renderTree: [],
    // 页面渲染数据结构
    addData: {},
    // 新增数据
    pathMap: {} // 节点层级路径

  },
  effects: {
    // 获取消息类型
    fetchMessageType: /*#__PURE__*/_regeneratorRuntime.mark(function fetchMessageType(_, _ref) {
      var call, put, messageTypeList;
      return _regeneratorRuntime.wrap(function fetchMessageType$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              call = _ref.call, put = _ref.put;
              _context.t0 = getResponse;
              _context.next = 4;
              return call(queryIdpValue, 'CMSG.MESSAGE_TYPE');

            case 4:
              _context.t1 = _context.sent;
              messageTypeList = (0, _context.t0)(_context.t1);

              if (!messageTypeList) {
                _context.next = 9;
                break;
              }

              _context.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  messageTypeList: messageTypeList
                }
              });

            case 9:
            case "end":
              return _context.stop();
          }
        }
      }, fetchMessageType);
    }),
    // 查询接收配置信息
    fetchReceiveConfig: /*#__PURE__*/_regeneratorRuntime.mark(function fetchReceiveConfig(_ref2, _ref3) {
      var payload, call, put, res, configList, _renderTreeData, pathMap, configKeys, flatKeys;

      return _regeneratorRuntime.wrap(function fetchReceiveConfig$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref2.payload;
              call = _ref3.call, put = _ref3.put;
              _context2.next = 4;
              return call(_fetchReceiveConfig, payload);

            case 4:
              res = _context2.sent;
              configList = getResponse(res);

              if (!configList) {
                _context2.next = 13;
                break;
              }

              _renderTreeData = renderTreeData(configList, {}), pathMap = _renderTreeData.pathMap;
              configKeys = [];

              flatKeys = function flatKeys(configItem) {
                if (_isArray(configItem.children) && !_isEmpty(configItem.children)) {
                  configKeys.push(configItem.receiveId);
                  configItem.children.forEach(function (item) {
                    return flatKeys(item);
                  });
                } else {
                  configKeys.push(configItem.receiveId);
                }
              };

              configList.forEach(function (item) {
                return flatKeys(item);
              });
              _context2.next = 13;
              return put({
                type: 'updateState',
                payload: {
                  configList: configList,
                  configKeys: configKeys,
                  pathMap: pathMap
                }
              });

            case 13:
            case "end":
              return _context2.stop();
          }
        }
      }, fetchReceiveConfig);
    }),
    // 保存下级配置
    saveConfig: /*#__PURE__*/_regeneratorRuntime.mark(function saveConfig(_ref4, _ref5) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function saveConfig$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref4.payload;
              call = _ref5.call;
              _context3.next = 4;
              return call(_saveConfig, payload);

            case 4:
              result = _context3.sent;
              return _context3.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context3.stop();
          }
        }
      }, saveConfig);
    }),
    // 编辑
    editConfig: /*#__PURE__*/_regeneratorRuntime.mark(function editConfig(_ref6, _ref7) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function editConfig$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref6.payload;
              call = _ref7.call;
              _context4.next = 4;
              return call(_editConfig, payload);

            case 4:
              result = _context4.sent;
              return _context4.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, editConfig);
    }),
    // 删除
    deleteConfig: /*#__PURE__*/_regeneratorRuntime.mark(function deleteConfig(_ref8, _ref9) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function deleteConfig$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref8.payload;
              call = _ref9.call;
              _context5.next = 4;
              return call(_deleteConfig, payload);

            case 4:
              result = _context5.sent;
              return _context5.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context5.stop();
          }
        }
      }, deleteConfig);
    })
  },
  reducers: {
    updateState: function updateState(state, action) {
      return _objectSpread(_objectSpread({}, state), action.payload);
    }
  }
};